package com.samoilovich.belarustoday.adapters;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.datemodel.SeanceModel;

import java.util.ArrayList;

public class SeancesAdapter extends BaseAdapter {
    private Activity mContext;
    private ArrayList<SeanceModel> mSeances;
    private int mLayoutRes;
    private View.OnClickListener mCinemaListener;

    public SeancesAdapter(Activity context, ArrayList<SeanceModel> seances, int layoutRes, View.OnClickListener listener) {
        mContext = context;
        mSeances = seances;
        mLayoutRes = layoutRes;
        mCinemaListener = listener;
    }

    public int getCount() {
        if (mSeances != null) {
            return mSeances.size();
        }

        return 0;
    }

    public SeanceModel getItem(int position) {
        if (mSeances != null && mSeances.size() > position) {
            return mSeances.get(position);
        }

        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        SeanceModel currentItem;
        Integer pos;

        pos = Integer.valueOf(position);
        currentItem = mSeances.get(position);
        if (convertView == null) {
            convertView = mContext.getLayoutInflater().inflate(mLayoutRes, null);
            holder = new ViewHolder();
            holder.Name = (TextView) convertView.findViewById(R.id.textView_cinemaName);
            holder.Place = (TextView) convertView.findViewById(R.id.textView_cinemaPlace);
            holder.SeanceTime = (TextView) convertView.findViewById(R.id.textView_seanceTime);
            holder.CinemaInfo = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_cinemaInfo);
            holder.SeanceTimeInfo = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_seanceInfo);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.CinemaInfo.setTag(pos);
        if (currentItem.isIsCinemaInfo()) {
            holder.CinemaInfo.setVisibility(View.VISIBLE);
            holder.SeanceTimeInfo.setVisibility(View.GONE);
            holder.Name.setText(currentItem.getCinema().getName());
            holder.Place.setText(currentItem.getCinema().getPlace());
            holder.CinemaInfo.setOnClickListener(mCinemaListener);
        } else {
            holder.CinemaInfo.setVisibility(View.GONE);
            holder.SeanceTimeInfo.setVisibility(View.VISIBLE);
            holder.SeanceTime.setText(currentItem.getTime());
        }

        holder.position = position;
        convertView.setTag(holder);

        return convertView;
    }

    public void setFilms(ArrayList<SeanceModel> seances) {
        mSeances = seances;
        notifyDataSetChanged();
    }

    public int getPosition(Object viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;

        return holder.position;
    }

    // -----------------------------------------------------------------------------------
    private class ViewHolder {
        TextView Name;
        TextView Place;
        TextView SeanceTime;
        RelativeLayout CinemaInfo;
        RelativeLayout SeanceTimeInfo;
        int position;
    }
}
